<?php
if ( count( get_included_files() ) == 1 ) die( '---' );

class PFLdocument
{
	// Constructeur
	function PFLdocument() 
        {
    	  $this->id = -1;
    	  $this->userId = 0;
    	  $this->description = "";
	  $this->chemin = "";
	  $this->fichier = "";
	  $this->repertoire = "Racine";
	  $this->type = "fichier";

	  global $tableDocument;    	
	  $this->table = $tableDocument;
        }

	// Load
	function load($documentId)
	{
	  $sql = "SELECT id, userId, chemin, description, fichier, repertoire, type FROM `".$this->table."` WHERE id = ".$documentId;
	  $data = claro_sql_query_get_single_row($sql);

	  if( !empty($data) )
	  {
	    $this->id     	= $data['id'];
	    $this->userId       = $data['userId'];
	    $this->chemin      	= $data['chemin'];
	    $this->fichier     	= $data['fichier'];
	    $this->description 	= $data['description'];
	    $this->repertoire 	= $data['repertoire'];
	    $this->type 	= $data['type'];
	   	
	    return true;
	  }
	  else
	  return false;
	}

	// Save 
	function save()  
	{
	   if( $this->id == -1 )
	   {
		$sql = "INSERT INTO `".$this->table."`
			SET userId = ".$this->userId.",
			    chemin = '".addslashes($this->chemin)."',
			    fichier = '".addslashes($this->fichier)."',
			    description = '".addslashes($this->description)."',
			    repertoire = '".addslashes($this->repertoire)."',
			    type = '".addslashes($this->type)."'
			";

		 $insertedId = claro_sql_query_insert_id($sql);

		 if( $insertedId )
		 {
		   $this->id = (int) $insertedId;
		   return $this->id;
		 }
		 else
		 {
		   return false;
		 }
	   }
	   else
	   {
		$sql = "UPDATE `".$this->table."`
			SET userId = ".$this->userId.",
			    chemin = '".addslashes($this->chemin)."',
			    fichier = '".addslashes($this->fichier)."',
			    description = '".addslashes($this->description)."',
			    repertoire = '".addslashes($this->repertoire)."',
			    type = '".addslashes($this->type)."'
			WHERE id = ".$this->id;

		 if( claro_sql_query($sql) )
		 {
		   return $this->id;
		 }
		 else
		 {
		   return false;
		 }
	   }
	}

	// Delete
	function delete()
	{
		$sql = "DELETE FROM `".$this->table."` WHERE id = ".$this->id;
		if( claro_sql_query($sql) == false ) return false;
			
		$this->id = -1;
		return true;
	}

	// ID
	function getId()
	{
		return (int) $this->id;
	}

	// UserId
	function getUserId()
	{
		return (int) $this->userId;
	}

	function setUserId($value)
	{
		$this->userId = $value;
	}

	// chemin (md5)
	function getChemin()
	{
		return $this->chemin;
	}

	function setChemin($value)
	{
		$this->chemin = $value;
	}

	// description
	function getDescription()
	{
		return $this->description;
	}

	function setDescription($value)
	{
		$this->description = $value;
	}

	// fichier
	function getFichier()
	{
		return $this->fichier;
	}

	function setFichier($value)
	{
		$this->fichier = $value;
	}

	// repertoire parent
	function getRepertoire()
	{
		return $this->repertoire;
	}

	function setRepertoire($value)
	{
		$this->repertoire = $value;
	}

	// type (répertoire / fichier)
	function getType()
	{
		return $this->type;
	}

	function setType($value)
	{
		$this->type = $value;
	}

	// Liste des docs
	function listeDocsUser()
	{
		$sql = "SELECT id 
			FROM `".$this->table."` 
			WHERE repertoire = '".$this->repertoire."'
			AND userId = ".$this->userId."
			ORDER BY type DESC, fichier";

		return claro_sql_query_fetch_all_rows($sql);
	}
}
?>
